<?php

namespace App\Http\Controllers\Test;

use App\Http\Controllers\Controller;
use App\Models\System\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use K12SSO_Factory;
use xmlrpc_server;
use xmlrpcresp;
use xmlrpcval;
use App\Models\System\Group;

class XmlrpcController extends Controller
{

    public function server()
    {
        Log::info('=======开始调用接口=========');
        $s = new xmlrpc_server([
            'group.syncGroup' => ['function' => self::class.'::syncGroup'],
            'user.syncUserExtend' => ['function' => self::class.'::syncUserExtend'],
        ], false);
        $s->compress_response = false;
        $s->response_charset_encoding = 'GBK';
        $s->service();
        Log::info('=======结束调用接口=========');
    }

    public static function syncGroup($m)
    {
        Log::info('=======开始同步组数据=========');
        $data = php_xmlrpc_decode($m->getParam(0));
        try
        {
            Group::saveGroupFromCenter($data);
        }
        catch(\Exception $e)
        {
            return Log::error($e);
        }
        Log::info('=======结束同步组数据=========');
        return new xmlrpcresp(new xmlrpcval(true, 'boolean'));
    }

    public static function syncUserExtend($m)
    {
        Log::info('=======开始同步用户数据=========');
        $data = php_xmlrpc_decode($m->getParam(0));
        try
        {
            User::saveUserFromCenter($data);
        }
        catch(\Exception $e)
        {
            return Log::error($e);
        }

        Log::info('=======结束同步用户数据=========');
        return new xmlrpcresp(new xmlrpcval(true, 'boolean'));
    }
}
